<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('检查记录') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('检查状态')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('检查状态')" clearable>
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="fileExamineRecordList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('检查文档')" align="center" prop="fileName" />
      <el-table-column :label="$t('检查状态')" align="center" prop="statusName" />
      <el-table-column :label="$t('检查时间')" align="center" prop="createTime" width="160" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === '2'" size="mini" type="text" @click="resultDownload(scope.row)">{{ $t('下载结果') }}</el-button>
          <el-button v-else size="mini" type="text" @click="queryResults(scope.row)">{{ $t('查询结果') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listFileExamineRecord } from '@/api/file/fileExamineRecord/fileExamineRecord'
import { getDocReviewResult } from '@/api/file/fileBase'
export default {
    components: {
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 对比数据表格数据
          fileExamineRecordList: [],
          fileExamineRecord: {
            id: undefined,
            fileId: undefined,
            openId: undefined,
            fileName: undefined,
            status: '',
            statusName: undefined,
            commentFileUrl: undefined
          },
          statusOptions: [
            // { dictValue: '0', dictLabel: '未审核' },
            { dictValue: '1', dictLabel: '审核中' },
            { dictValue: '2', dictLabel: '审核完成' }
            // { dictValue: '3', dictLabel: '回调完成' },
            // { dictValue: '4', dictLabel: '配额不足' }
          ],
          // 查询参数
          queryParams: {
              status: undefined,
              userId: this.$store.state.user.userId,
              pageNum: 1,
              pageSize: 10
          }
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询对比数据列表 */
    getList() {
        this.loading = true
        listFileExamineRecord(this.queryParams).then(response => {
            this.fileExamineRecordList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },
    // 查询文档检查结果
    queryResults(row) {
      // 根据文件id修改fileTag
      if (row.openId === null || row.openId === undefined || row.openId === '') {
        return
      }
      getDocReviewResult(row.openId).then(response => {
        if (response.code === 200) {
          this.fileExamineRecord = response.data
          this.getList()
        } else {
          this.msgError(response.msg)
        }
      })
    },
    downloadFile(url, fileName) {
      var x = new XMLHttpRequest()
      x.open('GET', url, true)
      x.responseType = 'blob'
      x.onload = function() {
        var url = window.URL.createObjectURL(x.response)
        var a = document.createElement('a')
        a.href = url
        a.download = fileName
        a.click()
      }
      x.send()
    },
    // 文件下载
    resultDownload(row) {
      const url = row.commentFileUrl
      var docAll = url.substring(url.lastIndexOf('/') + 1)
      var fileName = docAll.substring(0, docAll.lastIndexOf('.') + 1)
      this.downloadFile(url, fileName)
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    }

  }
}
</script>
